package com.backend.demo.dao;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.backend.demo.baseDAO.BaseDao;
import com.backend.demo.entities.Menu;
import com.backend.demo.page.PageBean;
import com.backend.demo.util.DateTimeUtil;
import com.backend.demo.util.StringUtils;

/**
 * 菜单DAO
 * @author Administrator
 *
 */
@SuppressWarnings("unchecked")
@Repository
public class MenuDao extends BaseDao<Menu, Long>{
	
	/**
	 * 查询所有的菜单
	 * @return
	 */
	public List<Menu> getAllMenus(){
		String hql = "from Menu";
		return getListByHQL(hql);
	}
	
	public PageBean<Menu> getAllMenusByPage(){
		String hql = "from Menu";
		String countHql = "select count(menuID) from Menu";
		PageBean<Menu> pageResults = findPageByFetchedHql(hql, countHql,1,5);
		return pageResults;
	}
	
	/**
	 * 根据查询条件查询菜单列表
	 * @param menu
	 * @return
	 */
	public List<Menu> getMenusByCondition(Menu menu){
		String hql = "from Menu where 1=1 ";
		StringBuffer stringBuffer = new StringBuffer(hql);
		if(StringUtils.isNotEmpty(menu.getMenu_zh_name())){
			stringBuffer.append("and menu_zh_name like '%" + menu.getMenu_zh_name()+"%'");
		}
		if(StringUtils.isNotEmpty(menu.getMenu_en_name())){
			stringBuffer.append("and menu_en_name like '%" + menu.getMenu_en_name()+"%'");
		}
		if(StringUtils.isNotEmpty(menu.getUrl())){
			stringBuffer.append("and url like '%" + menu.getUrl()+"%'");
		}
		if(StringUtils.isNotEmpty(menu.getImageURL())){
			stringBuffer.append("and imageURL like '%" + menu.getImageURL()+"%'");
		}
		if(menu.getCreatetime() != null){
			String format = DateTimeUtil.format("yyyy-MM-dd HH:mm:ss", menu.getCreatetime());
			stringBuffer.append("and createtime >='"+format+"'");
		}
		return getListByHQL(stringBuffer.toString());
	}
	
	/**
	 * 根据菜单ID查询二级菜单信息
	 * @param menu
	 * @return
	 */
	public Menu getMenuById(long menuID){
		String hql = "from Menu where menuID = ?";
		return getOneByHQL(hql, menuID);
	}
	
	/**
	 * 查询父菜单
	 * @return
	 */
	public List<Menu> getParentMenus(){
		String hql = "from Menu where parentID = 0";
		return getListByHQL(hql);
	}
	
	/**
	 * 查询子菜单
	 * @param parentID
	 * @return
	 */
	public List<Menu> getMenusByParentID(long parentID){
		String hql = "from Menu where parentID = ?";
		return getListByHQL(hql, parentID);
	}
	
	/**
	 * 新增菜单
	 * @param menu
	 * @return
	 */
	public int addMenu(Menu menu){
		menu.setCreatetime(new Date());
		menu.setUpdatetime(new Date());
		return save(menu);
	}
	
	/**
	 * 更新菜单
	 * @param menu
	 * @return
	 */
	public int updateMenu(Menu menu){
		String hql = "update Menu set menu_zh_name = ?,menu_en_name = ?,parentID = ?,url=?,imageURL = ?,updatetime = ? where menuID = ?";
		int num = updateOrDeleteByHql(hql, menu.getMenu_zh_name(),menu.getMenu_en_name(),menu.getParentID(),menu.getUrl(),menu.getImageURL(),new Date(),menu.getMenuID());
		return num;
	}
	
	/**
	 * 删除菜单
	 * @param menuID
	 * @return
	 */
	public int deleteMenu(long menuID){
		return deleteById(menuID);
	}

}
